from activist.Data import Data
from activist.MetaData import MetaData,MetaDataFactory
import activist.util


class PersonInformation(Data):

    __setattr__ = activist.util.writeOnceAttributes
    
    def __init__(self,person,name,phone,email,address,bio,private):
                                        # do not serialize -- created by Person
        if type(phone) is not list:
            phone = [phone]
        if type(email) is not list:
            email = [email]

        if not isinstance(name,basestring):
            raise "name must be a string "+str(type(name))
        self._name = name
        self._phone = phone
        self._email = email
        self._address = address
        self._bio = bio
        self._private = private

        self.id()
        super(PersonInformation,self).__init__()
        if person:
            MetaDataFactory(self,MetaData.InformationOf,person)

    def name(self):
        return self._name
    def phone(self):
        return self._phone
    def email(self):
        return self._email
    def address(self):
        return self._address
    def bio(self):
        return self._bio
    def private(self):
        return self._private

    def getXML(self):
        """this should probably make a Document first, then dump it to get
        nicer XML..."""
        
        rtn = """
<personinformation class="activist.PersonInformation" id="%(hash)s">
  <name>%(name)s</name>
  <address>%(address)s</address>
""" % {'hash':self._hash,
       'name':self._name,
       'address':self._address}

        for phone in self._phone:
            rtn = rtn + "  <phone>%s</phone>\n" % phone
        for email in self._email:
            rtn = rtn + "  <email>%s</email>\n" % email

        rtn = rtn + self.getMetaXML()
        return rtn + "</personinformation>\n"



    def __str__(self):
        return '<PersonInformation %s>' % self.id()
